package com.hxkcloud.patient.dborm;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;
import com.hxkcloud.patient.beans.DepartmentBean;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by william_sun on 2016-07-22.
 * Description:
 */
public class DepartmentDao {
    private Context context;
    private Dao<DepartmentBean, Integer> departmentBeanDao;

    public DepartmentDao(Context context) {
        this.context = context;
        try {
            departmentBeanDao = DatabaseHelper.getHelper(context).getDao(DepartmentBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<DepartmentBean> getDepartment() {
        try {
            return departmentBeanDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setDepartment(List<DepartmentBean> departs) {
        try {
            clearDepartment();
            for (DepartmentBean d : departs) {
                departmentBeanDao.create(d);
            }
        } catch (SQLException e) {
        }
    }

    public void clearDepartment() {
        try {
            TableUtils.clearTable(departmentBeanDao.getConnectionSource(), DepartmentBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
